package com.zs.hydrant.service.impl;

import com.zs.hydrant.entity.User;
import com.zs.hydrant.mapper.UserMapper;
import com.zs.hydrant.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String phone, String password, Integer role) {
        // 1. 根据手机号和角色查询用户
        User user = userMapper.selectByPhoneAndRole(phone, role);
        if (user == null) {
            return null; // 账号或角色不匹配
        }
        // 2. 比对密码（前端已MD5加密，直接与数据库密码对比）
        if (user.getPassword().equals(password)) {
            return user; // 密码正确，返回用户信息
        }
        return null; // 密码错误
    }

    @Override
    public List<User> list() {
        // 调用mapper层获取用户列表
        return userMapper.list();
    }

    @Override
    public boolean updateRoleByPhone(String phone, Integer role) {
        // 调用mapper层根据手机号更新用户角色
        int rows = userMapper.updateRoleByPhone(phone, role);
        return rows > 0; // 如果影响行数大于0，则更新成功
    }
    
    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }
    
    @Override
    public boolean removeById(Integer id) {
        int rows = userMapper.removeById(id);
        return rows > 0;
    }
    
    @Override
    public boolean updateById(User user) {
        int rows = userMapper.updateById(user);
        return rows > 0;
    }
    
    @Override
    public boolean updateStatusById(User user) {
        int rows = userMapper.updateStatusById(user);
        return rows > 0;
    }


}